"""
Rooms router for managing meeting rooms (Admin only).
"""
from typing import List, Annotated
from fastapi import APIRouter, Depends, Query, HTTPException, status
from sqlalchemy.orm import Session
from uuid import UUID
from app.database import get_db
from app.schemas.room import RoomCreate, RoomUpdate, RoomResponse
from app.services import room_service
from app.routers.auth import get_current_user, get_current_admin_user
from app.models.user import User

router = APIRouter()


@router.post("/", response_model=RoomResponse, status_code=status.HTTP_201_CREATED)
def create_room(
    room: RoomCreate,
    db: Session = Depends(get_db),
    current_admin: User = Depends(get_current_admin_user)
):
    """
    Create a new meeting room (Admin only).
    """
    return room_service.create_room(db, room)


@router.get("/", response_model=List[RoomResponse])
def get_rooms(
    skip: int = Query(0, ge=0),
    limit: int = Query(100, ge=1, le=1000),
    db: Session = Depends(get_db)
):
    """
    Get all meeting rooms.
    Public endpoint - no authentication required.
    """
    return room_service.get_rooms(db, skip=skip, limit=limit)


@router.get("/{room_id}", response_model=RoomResponse)
def get_room(
    room_id: UUID,
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_user)
):
    """
    Get a specific room by ID.
    """
    room = room_service.get_room(db, room_id)
    if not room:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="Room not found"
        )
    return room


@router.put("/{room_id}", response_model=RoomResponse)
def update_room(
    room_id: UUID,
    room_update: RoomUpdate,
    db: Session = Depends(get_db),
    current_admin: User = Depends(get_current_admin_user)
):
    """
    Update room information (Admin only).
    """
    return room_service.update_room(db, room_id, room_update)


@router.delete("/{room_id}")
def delete_room(
    room_id: UUID,
    db: Session = Depends(get_db),
    current_admin: User = Depends(get_current_admin_user)
):
    """
    Delete a meeting room (Admin only).
    Cannot delete if room has active bookings.
    """
    room_service.delete_room(db, room_id)
    return {"message": "Room deleted successfully"}
